package com.chuangjiangx.member.coupon.ddd.domain.repository;

import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.coupon.ddd.domain.model.*;
import com.chuangjiangx.member.basic.dao.mapper.InMbrWxCardMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrWxCard;
import com.chuangjiangx.member.basic.dao.model.InMbrWxCardExample;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 微信会员卡信息
 *
 * @author wp
 * @date 2018-08-28 17:30
 **/
@Component
public class MbrWxCardRepository implements Repository<MbrWxCard, MbrWxCardId> {

    private final InMbrWxCardMapper inMbrWxCardMapper;

    @Autowired
    public MbrWxCardRepository(InMbrWxCardMapper inMbrWxCardMapper) {
        this.inMbrWxCardMapper = inMbrWxCardMapper;
    }

    @Override
    public MbrWxCard fromId(MbrWxCardId mbrWxCardId) {
        InMbrWxCard inMbrWxCard = inMbrWxCardMapper.selectByPrimaryKey(mbrWxCardId.getId());
        return convertToDomain(inMbrWxCard);
    }

    public MbrWxCard fromMerchantId(MerchantId merchantId) {
        InMbrWxCardExample example = new InMbrWxCardExample();
        example.createCriteria().andMerchantIdEqualTo(merchantId.getId());
        List<InMbrWxCard> inMbrWxCardList = inMbrWxCardMapper.selectByExample(example);
        if (inMbrWxCardList != null && inMbrWxCardList.size() > 0) {
            return convertToDomain(inMbrWxCardList.get(0));
        }
        return null;
    }

    public MbrWxCard fromWxCardId(String wxCardId) {
        InMbrWxCardExample example = new InMbrWxCardExample();
        example.createCriteria().andWxCardIdEqualTo(wxCardId);
        List<InMbrWxCard> inMbrWxCardList = inMbrWxCardMapper.selectByExample(example);
        if (inMbrWxCardList != null && inMbrWxCardList.size() > 0) {
            return convertToDomain(inMbrWxCardList.get(0));
        }
        return null;
    }

    @Override
    public void update(MbrWxCard mbrWxCard) {
        InMbrWxCard inMbrWxCard = convertToIn(mbrWxCard);
        inMbrWxCardMapper.updateByPrimaryKeySelective(inMbrWxCard);
    }

    @Override
    public void save(MbrWxCard mbrWxCard) {
        InMbrWxCard inMbrWxCard = convertToIn(mbrWxCard);
        inMbrWxCardMapper.insertSelective(inMbrWxCard);
        mbrWxCard.setId(new MbrWxCardId(inMbrWxCard.getId()));
    }

    private MbrWxCard convertToDomain(InMbrWxCard inMbrWxCard) {
        MbrWxCard mbrWxCard = new MbrWxCard(
                new MerchantId(inMbrWxCard.getMerchantId()),
                inMbrWxCard.getShowEquityList()==null?null:ShowEquityEnum.fromCodeList(inMbrWxCard.getShowEquityList()),
                inMbrWxCard.getOperationInstruction(),
                inMbrWxCard==null?null:SwicthEnum.fromCode(inMbrWxCard.getInterfaceSwitch()),
                inMbrWxCard.getInterfaceName(),
                inMbrWxCard.getInterfaceGuide(),
                inMbrWxCard.getInterfaceUrl(),
                inMbrWxCard.getWxCardId(),
                inMbrWxCard.getWxRefuseReason(),
                inMbrWxCard.getWxStatus()==null?null:WxStatusEnum.fromCode(inMbrWxCard.getWxStatus()),
                inMbrWxCard.getWxAuditTime(),
                inMbrWxCard.getCreateTime(),
                inMbrWxCard.getUpdateTime()
        );
        mbrWxCard.setId(new MbrWxCardId(inMbrWxCard.getId()));
        return mbrWxCard;
    }

    private InMbrWxCard convertToIn(MbrWxCard mbrWxCard) {
        InMbrWxCard inMbrWxCard = new InMbrWxCard();
        inMbrWxCard.setId(mbrWxCard.getId() == null ? null : mbrWxCard.getId().getId());
        inMbrWxCard.setMerchantId(mbrWxCard.getMerchantId().getId());
        inMbrWxCard.setShowEquityList(ShowEquityEnum.toJsonList(mbrWxCard.getShowEquityList()));
        inMbrWxCard.setOperationInstruction(mbrWxCard.getOperationInstruction());
        inMbrWxCard.setInterfaceSwitch(mbrWxCard.getInterfaceSwitch()==null?null:mbrWxCard.getInterfaceSwitch().code);
        inMbrWxCard.setInterfaceName(mbrWxCard.getInterfaceName());
        inMbrWxCard.setInterfaceGuide(mbrWxCard.getInterfaceGuide());
        inMbrWxCard.setInterfaceUrl(mbrWxCard.getInterfaceUrl());
        inMbrWxCard.setWxCardId(mbrWxCard.getWxCardId());
        inMbrWxCard.setWxRefuseReason(mbrWxCard.getWxRefuseReason());
        inMbrWxCard.setWxStatus(mbrWxCard.getWxStatus()==null?null:mbrWxCard.getWxStatus().code);
        inMbrWxCard.setWxAuditTime(mbrWxCard.getWxAuditTime());
        inMbrWxCard.setCreateTime(mbrWxCard.getCreateTime());
        inMbrWxCard.setUpdateTime(mbrWxCard.getUpdateTime());
        return inMbrWxCard;
    }
}
